package com.alks.function.service.impl;

import com.alks.common.data.thread.UserIdThread;
import com.alks.entity.data.entity.PcMtlCheck;
import com.alks.entity.data.entity.PcSlipDetailSole;
import com.alks.function.mapper.PcSlipDetailSoleMapper;
import com.alks.function.mapper.stockdept.pcfactorystockquery.SD_SQ_PcDrawDetailMapper;
import com.alks.function.mapper.stockdept.pcfactorystockquery.SD_SQ_PcMtlAccMapper;
import com.alks.function.mapper.stockdept.pcfactorystockquery.SD_SQ_PcMtlCheckMapper;
import com.alks.function.service.IDarkDoorService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author MS
 * @since 2025/2/11下午5:25
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class DarkDoorServiceImpl implements IDarkDoorService {
    private final SD_SQ_PcMtlAccMapper accMapper;
    private final SD_SQ_PcMtlCheckMapper checkMapper;
    private final SD_SQ_PcDrawDetailMapper drawMapper;
    private final PcSlipDetailSoleMapper soleMapper;

    @Override
    public void accUp() {
        String companyId = UserIdThread.get().getCompanyId();
        /*写入全部现有材料*/
        accMapper.getAllMtl(companyId);
        /*补充出入库数*/
        accMapper.getAllInMtl(companyId);
    }

    @Override
    public void finance() {
        String companyId = UserIdThread.get().getCompanyId();
        /*获取待更新数据*/
        List<PcSlipDetailSole> soleList = soleMapper.getUnBalance(companyId);
        for (PcSlipDetailSole sole : soleList) {
            String[] split = sole.getBalanceNo().split(",");
            String balance = "空".equals(split[0])?split[1].replaceAll(" ",""):split[0].replaceAll(" ","");
            sole.setBalanceNo(balance);
        }
        /*刷新结算单号*/
        for (PcSlipDetailSole sole : soleList) {
            LambdaUpdateWrapper<PcSlipDetailSole> wrapper = new LambdaUpdateWrapper<PcSlipDetailSole>()
                    .eq(PcSlipDetailSole::getVendorName,sole.getVendorName())
                    .eq(PcSlipDetailSole::getLotNo,sole.getLotNo())
                    .eq(PcSlipDetailSole::getInOutType,sole.getInOutType())
                    .eq(PcSlipDetailSole::getFinanceYymm,sole.getFinanceYymm())
                    .set(PcSlipDetailSole::getBalanceNo,sole.getBalanceNo());
            soleMapper.update(null,wrapper);
        }
    }
}
